package uit.comstore.controller;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import uit.comstore.model.blo.DeliveryBLO;
import uit.comstore.model.blo.DeliveryDetailBLO;
import uit.comstore.model.blo.OrderBLO;
import uit.comstore.model.blo.OrderDetailBLO;
import uit.comstore.model.blo.ProductBLO;
import uit.comstore.model.entity.Delivery;
import uit.comstore.model.entity.DeliveryDetail;
import uit.comstore.model.entity.Order;
import uit.comstore.model.entity.OrderDetail;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class OrderAdminAction extends ActionSupport{
	private OrderBLO orderBLO;
	private List<Order> orderListNotDeliveried;
	private List<Order> orderListDeliveried;
	private Order order;

	private DeliveryBLO deliveryBLO;
	private List<Delivery> deliveryList;
	private Delivery delivery;
	
	private DeliveryDetailBLO deliveryDetailBLO;
	private List<DeliveryDetail> deliveryDetailList;
	private DeliveryDetail deliveryDetail;
	
	private OrderDetailBLO orderDetailBLO;
	private List<OrderDetail> orderDetailList;
	private OrderDetail orderDetail;
	
	private ProductBLO productBLO;
	
	private Long id;
	
	public OrderAdminAction(){
		orderBLO= new OrderBLO();
		deliveryBLO = new DeliveryBLO();
		deliveryDetailBLO = new DeliveryDetailBLO();
		orderDetailBLO = new OrderDetailBLO();
		productBLO = new ProductBLO();
	}
	
	public String delivery(){
		try{
			Order order = orderBLO.getById(getId());
			Delivery delivery = new Delivery();
			delivery.setDateDelivered(new Date());
			delivery.setOrder(order);
			delivery.setTotal(delivery.getOrder().getTotal());
			List<OrderDetail> orderDetails = orderDetailBLO.getById(getId());
			Set<DeliveryDetail> importDeliveryDetailSet = new HashSet<DeliveryDetail>();
			for(int i=0;i<orderDetails.size();i++){
				OrderDetail od = orderDetails.get(i);
				DeliveryDetail deliveryDetail = new DeliveryDetail();
				deliveryDetail.setDelivery(delivery);
				deliveryDetail.setProduct(productBLO.getById(od.getProduct().getId()));
				deliveryDetail.setProductPrice(od.getProductPrice());
				deliveryDetail.setProductQuantity(od.getProductQuantity());
				deliveryDetail.setTotal(od.getTotal());
				importDeliveryDetailSet.add(deliveryDetail);			
			}
			deliveryBLO.add(delivery);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return SUCCESS;
	}
	
	public List<DeliveryDetail> getDeliveryDetailList() {
		return deliveryDetailList;
	}

	public void setDeliveryDetailList(List<DeliveryDetail> deliveryDetailList) {
		this.deliveryDetailList = deliveryDetailList;
	}

	public DeliveryDetail getDeliveryDetail() {
		return deliveryDetail;
	}

	public void setDeliveryDetail(DeliveryDetail deliveryDetail) {
		this.deliveryDetail = deliveryDetail;
	}

	public String execute(){
		orderListNotDeliveried = orderBLO.listAllOrdersNotDelivery();
		orderListDeliveried = orderBLO.listAllOrdersDeliveried();
		deliveryList = deliveryBLO.list();
		orderDetailList = orderDetailBLO.list();
		deliveryDetailList = deliveryDetailBLO.list();
		return SUCCESS;
	}

	public List<Order> getOrderListNotDeliveried() {
		return orderListNotDeliveried;
	}

	public void setOrderListNotDeliveried(List<Order> orderListNotDeliveried) {
		this.orderListNotDeliveried = orderListNotDeliveried;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public List<Order> getOrderListDeliveried() {
		return orderListDeliveried;
	}

	public void setOrderListDeliveried(List<Order> orderListDeliveried) {
		this.orderListDeliveried = orderListDeliveried;
	}

	public List<Delivery> getDeliveryList() {
		return deliveryList;
	}

	public void setDeliveryList(List<Delivery> deliveryList) {
		this.deliveryList = deliveryList;
	}

	public Delivery getDelivery() {
		return delivery;
	}

	public void setDelivery(Delivery delivery) {
		this.delivery = delivery;
	}

	public List<OrderDetail> getOrderDetailList() {
		return orderDetailList;
	}

	public void setOrderDetailList(List<OrderDetail> orderDetailList) {
		this.orderDetailList = orderDetailList;
	}

	public OrderDetail getOrderDetail() {
		return orderDetail;
	}

	public void setOrderDetail(OrderDetail orderDetail) {
		this.orderDetail = orderDetail;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	
}
